package com.atguigu.gulimall.search.thread;

import com.google.common.util.concurrent.Runnables;

import java.util.concurrent.*;

/**
 * @作者：ldk
 * @email 2505496685@qq.com
 * @描述：默认描述
 * @创建时间：23:17 2020-11-15
 * @修改时间：23:17 2020-11-15
 * @修改描述：默认描述
 */
public class ThreadTest {
    public static ExecutorService executorService = Executors.newFixedThreadPool(10);
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main.....start....." );

//        Thread01 thread = new Thread01();
//        thread.start();

//        Runnable01 runnable01 = new Runnable01();
//        new Thread(runnable01).start();

//        FutureTask<Integer> futureTask = new FutureTask<>(new Callable01());
//        new Thread(futureTask).start();
//        //阻塞等待整个线程执行完成，获取返回结果
//        Integer integer = futureTask.get();
//        System.out.println("main.....end...."+integer);

        /**
         * corePoolSize:[5] 核心线程数【一直存在除非{@code allowCoreThreadTimeOut}】；程序池，创建好以后就准备就绪
         *
         *  maximumPoolSize：【200】最大线程数量；控制资源
         *  keepAliveTime：存活时间。如果当前的线程数量大于core数量。
         *          释放空闲的线程（maximumPoolSize-corePoolSize）。只要线程空闲大于指定的keepAliveTime；
         * unit,时间单位
         *BlockingQueue<Runnable> workQueue：阻塞队列。如果任务有很多，就会将目前多的任务放入队列里面
         *      只要有线程空闲，就会去队列里面取出新的任务继续执行
         *ThreadFactory threadFactory：线程的创建工厂
         *RejectedExecutionHandler handler：如果队列满了，按照我们指定的拒绝策略拒绝执行任务
         */

//        ThreadPoolExecutor executor = new ThreadPoolExecutor(5,
//                200,
//                10,
//                TimeUnit.SECONDS,
//                new LinkedBlockingDeque<>(1000),
//                Executors.defaultThreadFactory(),
//                new ThreadPoolExecutor.AbortPolicy());

//        CompletableFuture<Void> future  = CompletableFuture.runAsync(()->{
//            System.out.println("当前线程："+Thread.currentThread().getId() );
//            int i = 10/2;
//            System.out.println("运行结果："+i);
//        },executorService   );
//        System.out.println("main.....end....");

        //方法执行完成后的感知
//        CompletableFuture<Integer> future  = CompletableFuture.supplyAsync(()->{
//            System.out.println("当前线程："+Thread.currentThread().getId() );
//            int i = 10/2;
//            System.out.println("运行结果："+i);
//            return i;
//        },executorService).whenComplete((res,excption)->{
//            System.out.println("异步任务完成了，结果："+res+";异常："+excption);
//        }).exceptionally(throwable -> {
//            return 10;
//        });
        //方法执行完成后的处理
//        CompletableFuture<Integer> future  = CompletableFuture.supplyAsync(()->{
//            System.out.println("当前线程："+Thread.currentThread().getId() );
//            int i = 10/2;
//            System.out.println("运行结果："+i);
//            return i;
//        },executorService).handle((res,thr)->{
//            if(res!=null){
//                return res * 2;
//
//            }
//            if(thr!=null){
//                return 0;
//            }
//            return 0;

//        });
        /**
         * Integer integer = 0;
         *         try {
         *              integer = future.get();
         *         } catch (InterruptedException e) {
         *             e.printStackTrace();
         *         } catch (ExecutionException e) {
         *             e.printStackTrace();
         *         }
         */
//        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executorService).thenApplyAsync(res -> {
//            System.out.println("任务2驱动了" + res);
//            return "Hello" + res;
//        }, executorService);

        /**
         * 两个都完成
         */
//        CompletableFuture<Object> future01 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//            return i;
//        }, executorService);
//
//        CompletableFuture<Object> future02 = CompletableFuture.supplyAsync(() -> {
//            System.out.println("任务2线程：" + Thread.currentThread().getId());
//
//            try {
//                Thread.sleep(3000);
//                System.out.println("任务2结束：");
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            return "Hello";
//        }, executorService);

//        future01.runAfterBothAsync(future02,()->{
//            System.out.println("任务3开始");
//        },executorService);

//        future01.thenAcceptBothAsync(future02,(f1,f2)->{
//            System.out.println("任务3开始。。。之前的结果："+f1+"-------->"+f2);
//        },executorService);

//        CompletableFuture<String> future = future01.thenCombineAsync(future02, (f1, f2) -> {
//            return f1 + ":" + f2 + "----->Haha";
//        }, executorService);

        /**
         * 两个任务，主要由一个完成，我们就执行任务3
         *runAfterEitherAsync：不感知结果，自己也无返回值
         * acceptEitherAsync：感知结果，自己没有返回值
         * applyToEitherAsync：感知结果，自己有返回值
         */
//        future01.runAfterEitherAsync(future02,()->{
//            System.out.println("任务3开始。。。之前的结果：" );
//        },executorService);

//        future01.acceptEitherAsync(future02,(res)->{
//            System.out.println("任务3开始。。。之前的结果："+res );
//        },executorService);

//        CompletableFuture<String> future = future01.applyToEitherAsync(future02, res -> {
//            System.out.println("任务3开始。。。之前的结果：" + res);
//            return res.toString() + "----kqq";
//        }, executorService);

        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的图片信息");
            return "hello.jsp";
        },executorService);
        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的属性");
            return "黑色+256G";
        },executorService);

        CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
                System.out.println("查询商品介绍");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return "华为";
        },executorService);

//        CompletableFuture<Void> future = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
        CompletableFuture<Object> future = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);
        future.get();

//        System.out.println("main.....end...."  +futureImg.get()+"----->"+futureAttr.get()+"----->"+futureDesc.get());
        System.out.println("main.....end...."  +future.get());
    }



//    public static class Thread01 extends Thread{
//        @Override
//        public void run(){
//            System.out.println("当前线程："+Thread.currentThread().getId() );
//            int i = 10/2;
//            System.out.println("运行结果："+i);
//        }
//    }
//    public static class Runnable01 implements Runnable{
//
//
//        @Override
//        public void run() {
//            System.out.println("当前线程："+Thread.currentThread().getId() );
//            int i = 10/2;
//            System.out.println("运行结果："+i);
//        }
//    }


//    public static class Callable01 implements Callable<Integer>{
//
//        @Override
//        public Integer call() throws Exception {
//            System.out.println("当前线程："+Thread.currentThread().getId() );
//            int i = 10/2;
//            System.out.println("运行结果："+i);
//            return i;
//        }
//    }



}
